package com.yuanfeng.business.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.business.dto.HotMarketDTO;
import com.yuanfeng.business.dto.ShopClassInfoDTO;
import com.yuanfeng.business.dto.ShopClassQueryDTO;
import com.yuanfeng.business.entity.ShopClassEntity;
import com.yuanfeng.business.mapper.ShopClassMapper;
import com.yuanfeng.business.mapper.ShopJoinBaseMapper;
import com.yuanfeng.business.service.ShopClassService;
import com.yuanfeng.business.vo.BindGoodsListVO;
import com.yuanfeng.business.vo.DealerSetVO;
import com.yuanfeng.business.vo.GdbMerchantShopClassVO;
import com.yuanfeng.business.vo.ShopJoinExpenseVO;
import com.yuanfeng.commoms.constant.CommonStatusEnum;
import com.yuanfeng.commoms.enums.DealerSetKey;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseMapUtils;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ShopClassServiceImpl
 * @Description 店铺分类
 * @Author cf
 * @Date 2023/6/14 16:29
 * @Version 1.0
 */
@Service
public class ShopClassServiceImpl extends ServiceImpl<ShopClassMapper, ShopClassEntity> implements ShopClassService {

    private final static Logger log = LoggerFactory.getLogger(ShopClassServiceImpl.class);

    @Resource
    private ShopClassMapper shopClassMapper;

    @Resource
    private ShopJoinBaseMapper shopJoinBaseMapper;

    @Override
    public ResponseResult queryShopJoinExpense(Map<String, Object> param) {
        ResponseResult rr = new ResponseResult();
        try {
            List<ShopJoinExpenseVO> dataList= shopClassMapper.queryShopJoinExpense(param);
            for(int i=0;i < dataList.size();i++){
                dataList.get(i).setShopNum(i+1);
            }
            rr.setCode(CommonStatusEnum.SUCCESS.getCode());
            rr.setData(dataList);
            rr.setMessage(BizCodeEnume.OPERATION_SUCCESSFUL.getMsg());
        } catch (Exception e) {
            rr.setCode(CommonStatusEnum.FAIL.getCode());
            rr.setMessage(BizCodeEnume.UNKNOW_EXCEPTION.getMsg());
            log.error(e.getMessage());
        }
        return rr;
    }


    @Override
    public ResponseResult queryShopClassList(ShopClassQueryDTO param) {
        //分页
        //将对象转换为map
        Map data = null;
        try {
            data = ParseMapUtils.beanToMap(param);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        //分页查询数据
        IPage<GdbMerchantShopClassVO> page = this.baseMapper.selectShopClassList(new Query<GdbMerchantShopClassVO>().getPage(data),param);

        return ResponseResult.success(PageUtils.getPage(page));
    }

    @Override
    public ResponseResult updateShopClass(ShopClassInfoDTO param) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(param.getToken());
        if (userInfo == null) {
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL.getCode(), BizCodeEnume.TOKEN_IS_ILLEGAL.getMsg());
        }
        boolean flag;
        ShopClassEntity shopClass = JSON.parseObject(JSON.toJSONString(param), ShopClassEntity.class);
        shopClass.setShopPay(shopClass.getShopPay() == null ? shopClass.getShopClassDeposit() : shopClass.getShopPay());
        shopClass.setShopClassDeposit(shopClass.getShopClassDeposit() == null ? shopClass.getShopPay() : shopClass.getShopClassDeposit());
        //没有主键id就新增
        if (null == param.getShopClassId()) {
            flag = this.save(shopClass);
        } else {
            //更新店铺分类
            flag = this.updateById(shopClass);
        }
        if (flag) {
            return ResponseResult.success();
        }
        return ResponseResult.fail(BizCodeEnume.UPDATE_ERROR.getCode(), BizCodeEnume.UPDATE_ERROR.getMsg());
    }

    @Override
    public ResponseResult deleteShopClass(ShopClassQueryDTO param) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(param.getToken());
        if (userInfo == null) {
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL.getCode(), BizCodeEnume.TOKEN_IS_ILLEGAL.getMsg());
        }
        Integer shopClassId = shopJoinBaseMapper.queryShopClassId(param.getShopClassId());
        if (shopClassId > 0) {
            return ResponseResult.fail(0,"数据存在绑定关系，请解除绑定在删除");
        }
        //根据主键id删除数据
        boolean flag = this.removeById(param.getShopClassId());
        if (flag) {
            return ResponseResult.success();
        }
        return ResponseResult.fail(BizCodeEnume.DELETE_ERROR.getCode(), BizCodeEnume.DELETE_ERROR.getMsg());
    }

    @Override
    public ResponseResult queryShopClassDetail(ShopClassInfoDTO param) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(param.getToken());
        if (userInfo == null) {
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
        }
        ShopClassEntity result = this.getById(param.getShopClassId());
        return ResponseResult.success(result);
    }

    @Override
    public ResponseResult queryHotGoodsList(HotMarketDTO hotMarketRequest) {
        // 1.查询爆品折扣配置
        DealerSetVO dealerSet = this.baseMapper.queryDealerSet(DealerSetKey.hot_goods.name());
        if(null == dealerSet){
            return ResponseResult.fail(4001,"爆品集市已关闭");
        }
        Page<BindGoodsListVO> page = new Page<>(hotMarketRequest.getCurrentPage(), hotMarketRequest.getPageSize());
        IPage<BindGoodsListVO> bindGoodsList = this.baseMapper.queryHotBindGoodsList(page,hotMarketRequest);
        return ResponseResult.success(PageUtils.getPage(bindGoodsList));
    }

}
